import React, { Component } from 'react';
import './previewer.less';
import { Modal, Button, Icon } from 'tinper-bee';
import axios from 'axios';
import { vendorsDanger as danger } from 'utils/config.js';

function previewTemplate(param) {
    return axios({
        method: 'get',
        url: '/file/preview',
        params: param,
        headers: {
            'Cache-Control': 'no-cache'
        }
    })
}

//模版下载     /file/download
function downloadTemplate(param) {
    return axios({
        method: 'get',
        url: '/file/download',
        params: param,
        headers: {
            'Cache-Control': 'no-cache'
        }
    })
}

export default class Previewer extends Component {
    constructor(props) {
        super(props);
        this.state = {
            showViewModal: props.show
        };
    }

    componentWillReceiveProps(newProps) {
        let _me = this;
        if (!newProps.show) {
            _me.setState({
                showViewModal: newProps.show,
                url: ''
            })

            return;
        }
        if (newProps.viewossName) {
            _me.setState({
                viewossName: newProps.viewossName
            })
            previewTemplate({ ossName: newProps.viewossName, 'isAjax': 1, ieStamp: new Date().getTime() })
                .then(function (response) {
                    if (response.data.resultCode != 'busi_ex') {
                        let url = JSON.parse(response.data.result).data.viewUrl;
                        //window.open(url);
                        _me.setState({
                            showViewModal: newProps.show,
                            url: url
                        })
                    } else {
                        danger({ 'text': response.data.exMsg });
                    }
                });
        }
    }

    downloadFile = () => {
        downloadTemplate({ ossName: this.state.viewossName, 'isAjax': 1, ieStamp: new Date().getTime() })
            .then((response) => {
                if (response.data.resultCode != 'busi_ex') {
                    let url = response.data.result;
                    let a = document.createElement('a');
                    var filename = ''//record.datumContent;不知道为啥原来的人要把字段注释
                    a.href = url;
                    a.download = filename;
                    if (this.state.viewossName.toLowerCase().endsWith('.pdf')) {
                        a.target = '_blank';
                    }
                    console.log(a);
                    a.click();
                } else {
                    danger({ 'text': response.data.exMsg });
                }
            });
    }

    render() {
        return (
            <Modal
                show={this.state.showViewModal} onEscapeKeyUp={this.props.onClose}
                onHide={this.props.onClose} className='hviewmodal' backdrop={false} keyboard={true} >
                <Modal.Body className='hviewmodalbody'>
                    <div className='hviewop1'>
                        {/* <span onClick={this.props.onClose}>关闭</span> */}
                        <Icon onClick={this.props.onClose} type="uf-close" className='closeicon'></Icon>
                    </div>
                    <Button className='downloadbtn' colors="primary" onClick={this.downloadFile}>
                        <Icon type="uf-download" className='downloadicon'></Icon>
                        下载
                    </Button>
                    <iframe id='hviewiframe' className='hviewiframe' marginwidth='100px' frameBorder='0' src={this.state.url} />
                </Modal.Body>
            </Modal>
        )
    }
}